package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.adapter.MainSkinAdapter;
import com.hzdesk.media.koreabatchmatvector.bean.DeviceAttributeBean;
import com.hzdesk.media.koreabatchmatvector.custom.FlyBorderView;
import com.hzdesk.media.koreabatchmatvector.dao.DeviceAttributeBeanDaoUtils;
import com.hzdesk.media.koreabatchmatvector.inter.ISelectBgClick;
import com.hzdesk.media.koreabatchmatvector.logic.contract.MainContract;

import java.util.List;

/**
 * Created by Administrator on 2018/6/22.
 * 杭州席媒科技有限公司  Developers  刘传华  13
 * 佛祖镇楼 bug辟易
 */

public class MainPersenter implements MainContract.IMainPersenter {
    private MainContract.IMainView mMainView;
    private View pwView;
    private RecyclerView recyclerview;
    private FlyBorderView mfly;
    public Integer[] updateBg = new Integer[]{R.drawable.update_bg_01, R.drawable.update_bg_02,
            R.drawable.update_bg_03, R.drawable.update_bg_04, R.drawable.update_bg_05, R.drawable.update_bg_06};
    private PopupWindow popupWindow;
    private ImageView mivSkin;
    private Context mContext;
    private RelativeLayout rl_change_skin_selelctor_state_01;
    private RelativeLayout rl_change_skin_selelctor_state_02;
    private RelativeLayout rl_change_skin_selelctor_state_03;
    private RelativeLayout rl_change_skin_selelctor_state_04;
    private RelativeLayout rl_change_skin_selelctor_state_05;
    private RelativeLayout rl_change_skin_selelctor_state_06;
    private RelativeLayout rl_change_skin_bg_01;
    private RelativeLayout rl_change_skin_bg_02;
    private RelativeLayout rl_change_skin_bg_03;
    private RelativeLayout rl_change_skin_bg_04;
    private RelativeLayout rl_change_skin_bg_05;
    private RelativeLayout rl_change_skin_bg_06;

    private RelativeLayout mrlBg;

    public MainPersenter(MainContract.IMainView mMainView) {
        this.mMainView = mMainView;
    }

    private View.OnHoverListener onHoverListenerMeeting = new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    v.requestFocus();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    break;
            }
            return false;
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerMeeting = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                amplificationAnimator(v,0);
            }else{
                narrowAnimator(v,0);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerLocalVideo = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                amplificationAnimator(v,1);
            }else{
                narrowAnimator(v,1);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerLinCloud = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                amplificationAnimator(v,2);
            }else{
                narrowAnimator(v,2);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerLinSet = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                amplificationAnimator(v,3);
            }else{
                narrowAnimator(v,3);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerivSkin = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                mivSkin.setBackgroundResource(R.drawable.skin_01);
                onclickedSkinPopupWindow( mContext, mivSkin);
                amplificationAnimator(v,3);
            }else{
                mivSkin.setBackgroundResource(R.drawable.skin);
                if(popupWindow != null && popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
                narrowAnimator(v,3);
            }
        }
    };
    private Handler handle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    mMainView.changeTime();

                    break;
            }
        }
    };

    @Override
    public void setOnHoverListener(LinearLayout Meeting, LinearLayout LocalVideo, LinearLayout LinCloud, LinearLayout LinSet, FlyBorderView fly) {
        mfly = fly;
        Meeting.setOnHoverListener(onHoverListenerMeeting);
        LocalVideo.setOnHoverListener(onHoverListenerMeeting);
        LinCloud.setOnHoverListener(onHoverListenerMeeting);
        LinSet.setOnHoverListener(onHoverListenerMeeting);

        Meeting.setOnFocusChangeListener(OnFocusChangeListenerMeeting);
        LocalVideo.setOnFocusChangeListener(OnFocusChangeListenerLocalVideo);
        LinCloud.setOnFocusChangeListener(OnFocusChangeListenerLinCloud);
        LinSet.setOnFocusChangeListener(OnFocusChangeListenerLinSet);
    }

    @Override
    public void formatDate(Context mContext, RelativeLayout llBg) {
        mMainView.getDate();
        initPopupWindowView(mContext,llBg);
    }
    //
    private void initPopupWindowView(final Context mContext, final RelativeLayout llBg) {
        mrlBg = llBg;
        pwView = LayoutInflater.from(mContext).inflate(R.layout.main_change_skin, null);
        recyclerview = pwView.findViewById(R.id.main_change_skin_recyclerview);
        findSkinId(pwView);
       // allHidden();
      //  initialShow(mContext);
        GridLayoutManager layoutManager =  new GridLayoutManager(mContext,2);
        layoutManager.setOrientation(layoutManager.HORIZONTAL);
        recyclerview.setLayoutManager(layoutManager);

        final MainSkinAdapter mainSkinAdapter = new MainSkinAdapter(mContext);
        recyclerview.setAdapter(mainSkinAdapter);
        mainSkinAdapter.setmISelectBgClick(new ISelectBgClick() {
            @Override
            public void onItemClicke(int postion) {
                SharedPreferences SharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
                // todo 记录点击的是哪个
                SharedPreferences.Editor editor =   SharedPreferences.edit();
                editor.putInt("item_skin",postion);
                editor.commit();
                // todo 然后刷新试图
                mainSkinAdapter.notifyDataSetChanged();
                llBg.setBackgroundResource(updateBg[postion]);
            }
        });
    }



    private void findSkinId(View pwView) {
        rl_change_skin_bg_01 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_bg_01);
        rl_change_skin_bg_02 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_bg_02);
        rl_change_skin_bg_03 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_bg_03);
        rl_change_skin_bg_04 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_bg_04);
        rl_change_skin_bg_05 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_bg_05);
        rl_change_skin_bg_06 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_bg_06);


        rl_change_skin_selelctor_state_01 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_selelctor_state_01);
        rl_change_skin_selelctor_state_02 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_selelctor_state_02);
        rl_change_skin_selelctor_state_03 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_selelctor_state_03);
        rl_change_skin_selelctor_state_04 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_selelctor_state_04);
        rl_change_skin_selelctor_state_05 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_selelctor_state_05);
        rl_change_skin_selelctor_state_06 = pwView.findViewById(R.id.main_change_skin_rl_change_skin_selelctor_state_06);


        rl_change_skin_bg_01.setOnClickListener(clickedListenter);
        rl_change_skin_bg_02.setOnClickListener(clickedListenter);
        rl_change_skin_bg_03.setOnClickListener(clickedListenter);
        rl_change_skin_bg_04.setOnClickListener(clickedListenter);
        rl_change_skin_bg_05.setOnClickListener(clickedListenter);
        rl_change_skin_bg_06.setOnClickListener(clickedListenter);


        rl_change_skin_bg_01.setOnHoverListener(onHoverListenerMeeting);
        rl_change_skin_bg_02.setOnHoverListener(onHoverListenerMeeting);
        rl_change_skin_bg_03.setOnHoverListener(onHoverListenerMeeting);
        rl_change_skin_bg_04.setOnHoverListener(onHoverListenerMeeting);
        rl_change_skin_bg_05.setOnHoverListener(onHoverListenerMeeting);
        rl_change_skin_bg_06.setOnHoverListener(onHoverListenerMeeting);

        rl_change_skin_bg_01.setOnFocusChangeListener(OnFocusChangeListenerRlChangeSkinBg01);
        rl_change_skin_bg_02.setOnFocusChangeListener(OnFocusChangeListenerRlChangeSkinBg02);
        rl_change_skin_bg_03.setOnFocusChangeListener(OnFocusChangeListenerRlChangeSkinBg03);
        rl_change_skin_bg_04.setOnFocusChangeListener(OnFocusChangeListenerRlChangeSkinBg04);
        rl_change_skin_bg_05.setOnFocusChangeListener(OnFocusChangeListenerRlChangeSkinBg05);
        rl_change_skin_bg_06.setOnFocusChangeListener(OnFocusChangeListenerRlChangeSkinBg06);


    }

    @Override
    public void onClickedToDialogPager(int pager) {
        mMainView.toDialogPager(pager);
    }

    @Override
    public void onClickedRecordScreenLabeledPager() {
        mMainView.toRecordScreenLabeledPager();
    }

    @Override
    public void initTime() {
        // todo 每秒1秒格式化一次时间HH:mm
        new TimeThread().start();
    }

    @Override
    public void onclickedLoaclVido() {
        mMainView.toLoaclVidoPager();
    }

    @Override
    public void onClickedLinClude() {
        mMainView.toLinCludePager();
    }

    @Override
    public void onclickedSystemSettings() {
        mMainView.toSystemSettingsPager();
    }

    @Override
    public void onclickedSkinPopupWindow(Context mContext, final ImageView ivSkin) {
        popupWindow = new PopupWindow(pwView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        ColorDrawable cd = new ColorDrawable(0x00ffffff);// 背景颜色全透明
        popupWindow.setBackgroundDrawable(cd);
        popupWindow.setAnimationStyle(R.style.style_pop_animation);// 动画效果必须放在showAsDropDown()方法上边，否则无效
        popupWindow.showAsDropDown(ivSkin);
        if(popupWindow.isShowing()){
            ivSkin.setBackgroundResource(R.drawable.skin_01);
            initialShow(mContext);
        }
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ivSkin.setBackgroundResource(R.drawable.skin);
            }
        });
        //popupWindow.showAtLocation(tv_pop, Gravity.NO_GRAVITY, location[0]+tv_pop.getWidth(),location[1]);
    }

    @Override
    public void initSkinListener(ImageView ivSkin,Context tContext) {
        mivSkin = ivSkin;
        mContext = tContext;
        ivSkin.setOnHoverListener(onHoverListenerMeeting);
        ivSkin.setOnFocusChangeListener(OnFocusChangeListenerivSkin);
    }

    @Override
    public void showPersonalInformation(Context mContext) {
        // 从数据库获取url和用户名
        DeviceAttributeBeanDaoUtils mDeviceAttributeBeanDaoUtils = new DeviceAttributeBeanDaoUtils(mContext);
        List<DeviceAttributeBean> mlist = mDeviceAttributeBeanDaoUtils.equery();
        if (mlist != null && mlist.size() > 0) {
            String url = mlist.get(mlist.size() - 1).getImageUrl();
            ///Log.e("MainPersenter", "showPersonalInformation:url  "+url );
            String userName = mlist.get(mlist.size() - 1).getUserName();
          //  Log.e("MainPersenter", "showPersonalInformation:userName   "+userName );
            if ( userName != null && !TextUtils.isEmpty(userName)) {
                mMainView.showName(userName);

            }
            mMainView.showHeadPortrait(url);
        }
    }

    @Override
    public void onclickedToWiFi() {
        mMainView.toWiFiPager();
    }

    class TimeThread extends Thread {
        @Override
        public void run() {
            super.run();
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;
                    handle.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            } while (true);
        }
    }

    private void narrowAnimator(View v,int Type) {
        ObjectAnimator scaleX_01=ObjectAnimator.ofFloat(v,"scaleX",1.05f,1.0f);
        ObjectAnimator scaleY_01=ObjectAnimator.ofFloat(v,"scaleY",1.05f,1.0f);
        ObjectAnimator translationZ_01=ObjectAnimator.ofFloat(v,"translationZ",2.0f,0f);
        AnimatorSet animatorSet_01=new AnimatorSet();
        animatorSet_01.setDuration(200);
        animatorSet_01.play(scaleX_01).with(scaleY_01).with(translationZ_01);
        animatorSet_01.start();
        //mfly.attachToView(v,1.05f);
       // v.clearFocus();
        mMainView.hiddenBox(Type,v);
    }

    private void amplificationAnimator(View v,int Type) {
        ObjectAnimator scaleX=ObjectAnimator.ofFloat(v,"scaleX",1.0f,1.05f);
        ObjectAnimator scaleY=ObjectAnimator.ofFloat(v,"scaleY",1.0f,1.05f);
        ObjectAnimator translationZ=ObjectAnimator.ofFloat(v,"translationZ",0f,2.0f);
        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.play(scaleX).with(scaleY).with(translationZ);
        animatorSet.setDuration(200);
        animatorSet.start();
        mMainView.displayBox(Type,v);

    }

    private View.OnClickListener clickedListenter = new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.main_change_skin_rl_change_skin_bg_01:
                    allHidden();
                    rl_change_skin_selelctor_state_01.setVisibility(View.VISIBLE);
                    saveDate(0);
                    break;
                case R.id.main_change_skin_rl_change_skin_bg_02:
                    allHidden();
                    rl_change_skin_selelctor_state_02.setVisibility(View.VISIBLE);
                    saveDate(1);
                    break;
                case R.id.main_change_skin_rl_change_skin_bg_03:
                    allHidden();
                    rl_change_skin_selelctor_state_03.setVisibility(View.VISIBLE);
                    saveDate(2);
                    break;
                case R.id.main_change_skin_rl_change_skin_bg_04:
                    allHidden();
                    rl_change_skin_selelctor_state_04.setVisibility(View.VISIBLE);
                    saveDate(3);
                    break;
                case R.id.main_change_skin_rl_change_skin_bg_05:
                    allHidden();
                    rl_change_skin_selelctor_state_05.setVisibility(View.VISIBLE);
                    saveDate(4);
                    break;
                case R.id.main_change_skin_rl_change_skin_bg_06:
                    allHidden();
                    rl_change_skin_selelctor_state_06.setVisibility(View.VISIBLE);
                    saveDate(5);
                    break;
            }
        }
    };
    // 全部隐藏
    private void allHidden(){
        rl_change_skin_selelctor_state_01.setVisibility(View.GONE);
        rl_change_skin_selelctor_state_02.setVisibility(View.GONE);
        rl_change_skin_selelctor_state_03.setVisibility(View.GONE);
        rl_change_skin_selelctor_state_04.setVisibility(View.GONE);
        rl_change_skin_selelctor_state_05.setVisibility(View.GONE);
        rl_change_skin_selelctor_state_06.setVisibility(View.GONE);
    }
    // 保存数据
    private void saveDate(int postion){
        SharedPreferences SharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        // todo 记录点击的是哪个
        SharedPreferences.Editor editor =   SharedPreferences.edit();
        editor.putInt("item_skin",postion);
        editor.commit();
        mrlBg.setBackgroundResource(updateBg[postion]);
    }
    private void initialShow(Context mContext) {
        final SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        int selector = mSharedPreferences.getInt("item_skin",0); //todo 名称
        Log.e("initialShow", "initialShow: 名称 "+ selector);
        allHidden();
        switch (selector){
            case 0:
                rl_change_skin_bg_01.requestFocus();
                rl_change_skin_selelctor_state_01.setVisibility(View.VISIBLE);
                break;
            case 1:
                rl_change_skin_bg_02.requestFocus();
                rl_change_skin_selelctor_state_02.setVisibility(View.VISIBLE);
                break;
            case 2:
                rl_change_skin_bg_03.requestFocus();
                rl_change_skin_selelctor_state_03.setVisibility(View.VISIBLE);
                break;
            case 3:
                rl_change_skin_bg_04.requestFocus();
                rl_change_skin_selelctor_state_04.setVisibility(View.VISIBLE);
                break;
            case 4:
                rl_change_skin_bg_05.requestFocus();
                rl_change_skin_selelctor_state_05.setVisibility(View.VISIBLE);
                break;
            case 5:
                rl_change_skin_bg_06.requestFocus();
                rl_change_skin_selelctor_state_06.setVisibility(View.VISIBLE);
                break;
        }
        saveDate(selector);
    }

    private View.OnFocusChangeListener OnFocusChangeListenerRlChangeSkinBg01 = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                allHidden();
                rl_change_skin_selelctor_state_01.setVisibility(View.VISIBLE);
                saveDate(0);
            }else{
                rl_change_skin_selelctor_state_01.setVisibility(View.GONE);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerRlChangeSkinBg02 = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                allHidden();
                rl_change_skin_selelctor_state_02.setVisibility(View.VISIBLE);
                saveDate(1);
            }else{
                rl_change_skin_selelctor_state_02.setVisibility(View.GONE);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerRlChangeSkinBg03 = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                allHidden();
                rl_change_skin_selelctor_state_03.setVisibility(View.VISIBLE);
                saveDate(2);
            }else{
                rl_change_skin_selelctor_state_03.setVisibility(View.GONE);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerRlChangeSkinBg04 = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                allHidden();
                rl_change_skin_selelctor_state_04.setVisibility(View.VISIBLE);
                saveDate(3);
            }else{
                rl_change_skin_selelctor_state_04.setVisibility(View.GONE);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerRlChangeSkinBg05 = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                allHidden();
                rl_change_skin_selelctor_state_05.setVisibility(View.VISIBLE);
                saveDate(4);
            }else{
                rl_change_skin_selelctor_state_05.setVisibility(View.GONE);
            }
        }
    };
    private View.OnFocusChangeListener OnFocusChangeListenerRlChangeSkinBg06 = new View.OnFocusChangeListener(){

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){ // 选中
                allHidden();
                rl_change_skin_selelctor_state_06.setVisibility(View.VISIBLE);
                saveDate(5);
            }else{
                rl_change_skin_selelctor_state_06.setVisibility(View.GONE);
            }
        }
    };

}
